let eo = require('../../../common/lib/mobx').extendObservable;
let conv = require("../../../util/op_code").conv

let random_store = function () {
	eo(this, {
		/* 
		{
			"letter":		字符
			"state":		是否正确
			"letter_style":	显示状态
				"w_letter_color_current"	当前字符
				"w_letter_color_right"		输入正确	
				"w_letter_color_wrong"		输入错误
		} 
		*/
		train_data: [
		],
		// 训练数据个数
		train_num: 12 * 6,
		// 训练数据集
		train_set: [],
		current_index: 0,
		
		// 输入的字符
		input_code: {
			show_code: "",
			unit_code: "",
		},
		right_code: 0,
		wrong_code: 0,
		right_rate: "",
		// 显示提示
		show_tip: true,
		tip_code: "",

	});
	// 处理字符
	this.code_op = function(msg) {
		if(msg.type == "inp") {
			this.input_code.show_code += msg.code
			if(msg.code != " ") {
				this.input_code.unit_code += msg.code
			}
			else if(msg.code == " ") {
				this.check_letter()
			}
		}
	}
	this.check_letter = function() {
		if(this.input_code.unit_code == "") {
			return
		}
		let w = conv("c2w", this.input_code.unit_code)
		this.input_code.show_code += w
		this.input_code.unit_code = ""
		// 判断是否键入正确
		if(w == this.train_data[this.current_index].letter) {
			this.train_data[this.current_index].state = true
			this.train_data[this.current_index].letter_style = "w_letter_color_right"
		}
		else {
			this.train_data[this.current_index].state = false
			this.train_data[this.current_index].letter_style = "w_letter_color_wrong"
		}
		if (this.current_index < this.train_data.length - 1) {
			this.current_index ++
		}
		this.ref_current_state()
	}
	// 刷新当前显示状态
	this.ref_current_state = function() {
		// 当前字符样式改变
		this.train_data[this.current_index].letter_style = "w_letter_color_current"
		// 显示提示
		this.ref_tip_code()
		
		// 计算正确率
		this.calc_rate()
	}
	// 计算正确率
	this.calc_rate = function() {
		let right_num = 0;
		let total_num = 0;
		for(let i = 0; i < this.current_index; i ++) {
			total_num ++
			if(this.train_data[i].state) {
				right_num ++
			}
		}
		this.right_code = right_num
		this.wrong_code = total_num - right_num
		if (total_num != 0) {
			this.right_rate = String(((right_num / total_num) * 100).toFixed(2)) + " %"
		}
		else {
			this.right_rate = "0.00 %"
		}
		
	}
	// 获取提示字符
	this.ref_tip_code = function() {
		if (this.show_tip) {
			let c = conv("w2c", this.train_data[this.current_index].letter)
			this.tip_code = this.train_data[this.current_index].letter + " " + c
		}
		else {
			this.tip_code = ""
		}
	}
}

module.exports = {
	random_store: new random_store
}

// export default common_store
